clear all
set more off

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2




*===============================================================================
*							EXPECTED INFLATION
*===============================================================================
cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear


*** winsorize/drop unrealistic observations
winsor2 epi_12m, cuts(2 98) suffix(_w)
winsor2 epi_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen epi_12m_w_X_IT=epi_12m_w*treatment_IT
gen epi_12m_w_X_AIT=epi_12m_w*treatment_AIT

gen epi_12m_mean_X_IT=epi_12m_mean*treatment_IT
gen epi_12m_mean_X_AIT=epi_12m_mean*treatment_AIT


***=============================================================================
*** binscatter plot for treatment and control groups
***=============================================================================

foreach var in epi_5y_w epi_12m_w treatment_IT treatment_AIT epi_12m_w_X_IT epi_12m_w_X_AIT {
    gen _`var'=`var'*sqrt(weight)
}
gen _W=sqrt(weight)
rreg2 _epi_5y_w _epi_12m_w _treatment_IT _treatment_AIT _epi_12m_w_X_IT _epi_12m_w_X_AIT _W, nolog gen(wgtXb2)
replace wgtXb2=wgtXb2*weight

reg epi_5y_w epi_12m_w treatment_IT treatment_AIT epi_12m_w_X_IT epi_12m_w_X_AIT, robust

reg epi_5y_w epi_12m_w treatment_IT treatment_AIT epi_12m_w_X_IT epi_12m_w_X_AIT [aw=wgtXb2], robust

local bins0=30
quiet forvalues var=1(1)3 {
	xtile c_`var'=epi_12m_w [aw=wgtXb2] if treatmentN==`var', nq(`bins0')
	egen x_`var'=mean(epi_12m_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen y_`var'=mean(epi_5y_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen w_`var'=mean(wgtXb2) if treatmentN==`var', by(c_`var')
 	replace x_`var'=x_`var'/ w_`var'
 	replace y_`var'=y_`var'/ w_`var'	
}

	xtile c_00=epi_12m_w  [aw=wgtXb2], nq(`bins0')
	egen x_00=mean(epi_12m_w) , by(c_00)

forvalues var=1(1)3 {
	sort  x_`var'
	by x_`var': gen _t_`var'=_n
}
	
	sort  x_00
	by x_00: gen _t_00=_n
	
cd "C:\Dropbox\Average_target_survey\replication_folder\figures"	


twoway ///
		/// CONTROL GROUP
		(scatter y_1 x_1 if _t_1==1, msymbol(th) mcolor(black)) ///
		(lfit epi_5y_w epi_12m_w  [aw=wgtXb2] if treatmentN==1 , lcolor(black)) ///
		/// TREATMENT GROUP: IT
		(scatter y_2 x_2 if _t_2==1, msymbol(sh) mcolor(red) ) ///
		(lfit epi_5y_w epi_12m_w  [aw=wgtXb2] if treatmentN==2 , lcolor(red) lpattern(shortdash)) ///
		/// TREATMENT GROUP: AIT
		(scatter y_3 x_3 if _t_3==1, msymbol(oh) mcolor(blue) ) ///
		(lfit epi_5y_w epi_12m_w  [aw=wgtXb2] if treatmentN==3 , lcolor(blue) lpattern(longdash)) ///		
		, ///
		legend(off) ///
		xtitle("prior E{&pi}, 1-year-ahead inflation", size(small)) ///
		xlabel(-50(25)75, labsize(small)) ///
		ylabel(, labsize(small)) ///
		ytitle("posterior E{&pi}, 5-year-ahead inflation", size(small)) ///
		graphregion(color(white)) bgcolor(white) ///
		title("{bf: Panel A: Expected inflation}", size(medium)) ///
		saving(RCT_epi.gph, replace)
		

		
		
*===============================================================================
*					PROB OF HIGH FUTURE INFLATION
*===============================================================================		
		
cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

*** probability that inflation is greater than 4 percent (prior)
gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8

*** probability that inflation is greater than 5 percent (posterior)
gen prob_post=QJH18_1

*** winsorize/drop unrealistic observations
winsor2 prob_post, cuts(2 98) suffix(_w)
winsor2 prob_prior, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen prob_prior_w_X_IT=prob_prior_w*treatment_IT
gen prob_prior_w_X_AIT=prob_prior_w*treatment_AIT



foreach var in prob_post_w prob_prior_w treatment_IT treatment_AIT prob_prior_w_X_IT prob_prior_w_X_AIT {
    gen _`var'=`var'*sqrt(weight)
}
gen _W=sqrt(weight)
rreg2 _prob_post_w _prob_prior_w _treatment_IT _treatment_AIT _prob_prior_w_X_IT _prob_prior_w_X_AIT _W, nolog gen(wgtXb2)
replace wgtXb2=wgtXb2*weight


reg prob_post_w prob_prior_w treatment_IT treatment_AIT prob_prior_w_X_IT prob_prior_w_X_AIT, robust

reg prob_post_w prob_prior_w treatment_IT treatment_AIT prob_prior_w_X_IT prob_prior_w_X_AIT [aw=wgtXb2], robust

local bins0=30
quiet forvalues var=1(1)3 {
	xtile c_`var'=prob_prior_w [aw=wgtXb2] if treatmentN==`var', nq(`bins0')
	egen x_`var'=mean(prob_prior_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen y_`var'=mean(prob_post_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen w_`var'=mean(wgtXb2) if treatmentN==`var', by(c_`var')
 	replace x_`var'=x_`var'/ w_`var'
 	replace y_`var'=y_`var'/ w_`var'	
}

	xtile c_00=prob_prior_w  [aw=wgtXb2], nq(`bins0')
	egen x_00=mean(prob_prior_w) , by(c_00)

forvalues var=1(1)3 {
	sort  x_`var'
	by x_`var': gen _t_`var'=_n
}
	
	sort  x_00
	by x_00: gen _t_00=_n
	
cd "C:\Dropbox\Average_target_survey\replication_folder\figures"	

twoway ///
		/// CONTROL GROUP
		(scatter y_1 x_1 if _t_1==1, msymbol(th) mcolor(black)) ///
		(lpoly prob_post_w prob_prior_w  [aw=wgtXb2] if treatmentN==1 , lcolor(black)) ///
		/// TREATMENT GROUP: IT
		(scatter y_2 x_2 if _t_2==1, msymbol(sh) mcolor(red) ) ///
		(lpoly prob_post_w prob_prior_w  [aw=wgtXb2] if treatmentN==2 , lcolor(red) lpattern(shortdash)) ///
		/// TREATMENT GROUP: AIT
		(scatter y_3 x_3 if _t_3==1, msymbol(oh) mcolor(blue) ) ///
		(lpoly prob_post_w prob_prior_w  [aw=wgtXb2] if treatmentN==3 , lcolor(blue) lpattern(longdash)) ///		
		, ///
		legend(order(2 4 6) ///
			label(2 "Control") ///
			label(4 "Treatment (IT)") ///
			label(6 "Treatment (average IT)") ///
			ring(0) position(5) rows(3) rowgap(0.2) size(small)) ///		xtitle("prior: probability  1-year-ahead expected inflation >4%", size(small)) ///
		ytitle("posterior: probability  1-year-ahead expected inflation >5%", size(small)) ///
		xtitle("posterior: probability  1-year-ahead expected inflation >4%", size(small)) ///
		xlabel(, labsize(small)) ///
		ylabel(, labsize(small)) ///			
		graphregion(color(white)) bgcolor(white)   ///
		title("{bf: Panel D: Expected prob. of high inflation}", size(medium)) ///
		saving(RCT_epi_high.gph, replace)
				
		
		
*===============================================================================
*			EXPECtED GDP GROWTH
*===============================================================================

cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

winsor2 egdp_12m, cuts(2 98) suffix(_w)
winsor2 egdp_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen egdp_12m_w_X_IT=egdp_12m_w*treatment_IT
gen egdp_12m_w_X_AIT=egdp_12m_w*treatment_AIT

foreach var in egdp_5y_w egdp_12m_w treatment_IT treatment_AIT egdp_12m_w_X_IT egdp_12m_w_X_AIT {
    gen _`var'=`var'*sqrt(weight)
}
gen _W=sqrt(weight)
rreg2 _egdp_5y_w _egdp_12m_w _treatment_IT _treatment_AIT _egdp_12m_w_X_IT _egdp_12m_w_X_AIT _W, nolog gen(wgtXb2)
replace wgtXb2=wgtXb2*weight

reg egdp_5y_w egdp_12m_w treatment_IT treatment_AIT egdp_12m_w_X_IT egdp_12m_w_X_AIT, robust

reg egdp_5y_w egdp_12m_w treatment_IT treatment_AIT egdp_12m_w_X_IT egdp_12m_w_X_AIT [aw=wgtXb2], robust

local bins0=30
quiet forvalues var=1(1)3 {
	xtile c_`var'=egdp_12m_w [aw=wgtXb2] if treatmentN==`var', nq(`bins0')
	egen x_`var'=mean(egdp_12m_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen y_`var'=mean(egdp_5y_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen w_`var'=mean(wgtXb2) if treatmentN==`var', by(c_`var')
 	replace x_`var'=x_`var'/ w_`var'
 	replace y_`var'=y_`var'/ w_`var'	
}

	xtile c_00=egdp_12m_w  [aw=wgtXb2], nq(`bins0')
	egen x_00=mean(egdp_12m_w) , by(c_00)

forvalues var=1(1)3 {
	sort  x_`var'
	by x_`var': gen _t_`var'=_n
}
	
	sort  x_00
	by x_00: gen _t_00=_n

cd "C:\Dropbox\Average_target_survey\replication_folder\figures"	

twoway ///
		/// CONTROL GROUP
		(scatter y_1 x_1 if _t_1==1, msymbol(th) mcolor(black)) ///
		(lfit egdp_5y_w egdp_12m_w [aw=wgtXb2]   if treatmentN==1 , lcolor(black) ) ///
		/// TREATMENT GROUP: IT
		(scatter y_2 x_2 if _t_2==1, msymbol(sh) mcolor(red) ) ///
		(lfit egdp_5y_w egdp_12m_w [aw=wgtXb2]  if treatmentN==2 , lcolor(red) lpattern(shortdash) ) ///
		/// TREATMENT GROUP: AIT
		(scatter y_3 x_3 if _t_3==1, msymbol(oh) mcolor(blue) ) ///
		(lfit egdp_5y_w egdp_12m_w [aw=wgtXb2]  if treatmentN==3 , lcolor(blue) lpattern(longdash) ) ///		
		, ///
		legend(off) ///
		xtitle("prior E({&Delta}GDP), 1-year-ahead GDP growth", size(small)) ///
		ytitle("posterior E({&Delta}GDP), 5-year-ahead GDP growth", size(small)) ///
		xlabel(-50(25)75, labsize(small)) ///
		ylabel(, labsize(small)) ///		
		graphregion(color(white)) bgcolor(white)   ///
		title("{bf: Panel B: Expected GDP growth}", size(medium)) ///
		saving(RCT_egdp.gph, replace)		
		
		
*===============================================================================
*				PERSONAL INCOME GROWTH
*===============================================================================
cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

winsor2 edy_12m, cuts(2 98) suffix(_w)
winsor2 edy_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen edy_12m_w_X_IT=edy_12m_w*treatment_IT
gen edy_12m_w_X_AIT=edy_12m_w*treatment_AIT

foreach var in edy_5y_w edy_12m_w treatment_IT treatment_AIT edy_12m_w_X_IT edy_12m_w_X_AIT  {
    gen _`var'=`var'*sqrt(weight)
}
gen _W=sqrt(weight)
rreg2 _edy_5y_w _edy_12m_w _treatment_IT _treatment_AIT _edy_12m_w_X_IT _edy_12m_w_X_AIT  _W, nolog gen(wgtXb2)
replace wgtXb2=wgtXb2*weight

reg edy_5y_w edy_12m_w treatment_IT treatment_AIT edy_12m_w_X_IT edy_12m_w_X_AIT, robust

reg edy_5y_w edy_12m_w treatment_IT treatment_AIT edy_12m_w_X_IT edy_12m_w_X_AIT [aw=wgtXb2], robust

local bins0=30
quiet forvalues var=1(1)3 {
	xtile c_`var'=edy_12m_w [aw=wgtXb2] if treatmentN==`var', nq(`bins0')
	egen x_`var'=mean(edy_12m_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen y_`var'=mean(edy_5y_w*wgtXb2) if  treatmentN==`var', by(c_`var')
	egen w_`var'=mean(wgtXb2) if treatmentN==`var', by(c_`var')
 	replace x_`var'=x_`var'/ w_`var'
 	replace y_`var'=y_`var'/ w_`var'	
}

	xtile c_00=edy_12m_w  [aw=wgtXb2], nq(`bins0')
	egen x_00=mean(edy_12m_w) , by(c_00)

forvalues var=1(1)3 {
	sort  x_`var'
	by x_`var': gen _t_`var'=_n
}
	
	sort  x_00
	by x_00: gen _t_00=_n
	
cd "C:\Dropbox\Average_target_survey\replication_folder\figures"	

twoway ///
		/// CONTROL GROUP
		(scatter y_1 x_1 if _t_1==1, msymbol(th) mcolor(black)) ///
		(lfit edy_5y_w edy_12m_w [aw=wgtXb2]   if treatmentN==1 , lcolor(black) ) ///
		/// TREATMENT GROUP: IT
		(scatter y_2 x_2 if _t_2==1, msymbol(sh) mcolor(red) ) ///
		(lfit edy_5y_w edy_12m_w [aw=wgtXb2]  if treatmentN==2 , lcolor(red) lpattern(shortdash) ) ///
		/// TREATMENT GROUP: AIT
		(scatter y_3 x_3 if _t_3==1, msymbol(oh) mcolor(blue) ) ///
		(lfit edy_5y_w edy_12m_w [aw=wgtXb2]  if treatmentN==3 , lcolor(blue) lpattern(longdash) ) ///		
		, ///
		legend(off) ///
		xtitle("prior E({&Delta}DI), 1-year-ahead disposable income growth", size(small)) ///
		ytitle("posterior E({&Delta}DI), 5-year-ahead disposable income growth", size(small)) ///
		xlabel(-50(25)100, labsize(small)) ///
		ylabel(, labsize(small)) ///			
		graphregion(color(white)) bgcolor(white)   ///
		title("{bf: Panel C: Expected personal income growth}", size(medium)) ///
		saving(RCT_edy.gph, replace)
			

*===============================================================================
*							COMBINE FIGURES
*===============================================================================

cd "C:\Dropbox\Average_target_survey\replication_folder\figures"	


graph combine 	RCT_epi.gph ///
				RCT_egdp.gph ///
				RCT_edy.gph ///
				RCT_epi_high.gph ///
	, rows(2) imargin(tiny) graphregion(color(white)) ///
		ysize(20) xsize(28)
